﻿/******************************************************************
** 文件名:T106_GoodClassStockPropertyDAL.cs
** Copyright (c) 
** 创建人:佟礼
** 日 期:2012-07-10 17:44:32
** 描 述:数据操作类
********************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using System.Data;
using Enter.DataProxy.DbConnector;
using Enter.Entity;
using Enter.DataProxy.Mail.Base;
using Enter.Config;
namespace Enter.DataProxy.Mail
{
    /// <summary>
    /// 数据操作类：T106_GoodClassStockPropertyManager
    /// </summary>
    public class T106_GoodClassStockPropertyManager : DataProxyManager, IT106_GoodClassStockPropertyManager
    {
        public T106_GoodClassStockPropertyManager() :
            base(ProxyProvider.ConfigurationName, ProxyProvider.ConfiruationDbName) { }
        #region 插入记录
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <returns></returns>
        public int Insert(T106_GoodClassStockPropertyEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            var db = base.CreateDbWriter();
            strSql.Append("INSERT INTO T106_GoodClassStockProperty");
            strSql.Append("(PNo,PName,Color,icon,ParentNO,Descr,Status,ProType,IsRequired)");
            strSql.Append(" values ");
            strSql.Append("(@PNo,@PName,@Color,@icon,@ParentNO,@Descr,@Status,@ProType,@IsRequired)");
            ParameterCollection parameters = CreateParameter();
            parameters.Append("PNo", model.PNo, DbType.String);
            parameters.Append("PName", model.PName, DbType.String);
            parameters.Append("Color", model.Color, DbType.String);
            parameters.Append("icon", model.icon, DbType.String);
            parameters.Append("ParentNO", model.ParentNO, DbType.String);
            parameters.Append("Descr", model.Descr, DbType.String);
            parameters.Append("Status", model.Status, DbType.Int32);
            parameters.Append("ProType", model.ProType, DbType.Int32);
            parameters.Append("IsRequired", model.IsRequired, DbType.Boolean);
            return db.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        #endregion

        #region 更新数据
        /// <summary>
        /// 更新数据
        /// </summary>
        /// <returns></returns>
        public int Update(T106_GoodClassStockPropertyEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            var db = base.CreateDbWriter();
            strSql.Append("UPDATE T106_GoodClassStockProperty SET ");
            strSql.Append("PNo=@PNo,PName=@PName,Color=@Color,icon=@icon,ParentNO=@ParentNO,Descr=@Descr,Status=@Status,ProType=@ProType,IsRequired=@IsRequired");
            strSql.Append(" WHERE PID=@PID");
            ParameterCollection parameters = CreateParameter();
            parameters.Append("PID", model.PID, DbType.Int32);
            parameters.Append("PNo", model.PNo, DbType.String);
            parameters.Append("PName", model.PName, DbType.String);
            parameters.Append("Color", model.Color, DbType.String);
            parameters.Append("icon", model.icon, DbType.String);
            parameters.Append("ParentNO", model.ParentNO, DbType.String);
            parameters.Append("Descr", model.Descr, DbType.String);
            parameters.Append("Status", model.Status, DbType.Int32);
            parameters.Append("ProType", model.ProType, DbType.Int32);
            parameters.Append("IsRequired", model.IsRequired, DbType.Boolean);
            return db.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        #endregion

        #region IEntityFacade 成员
        /// <summary>
        /// 填充数据
        /// </summary>
        /// <param name="reader"></param>
        public T106_GoodClassStockPropertyEntity FillDetailWithReader(IDataReader reader)
        {
            T106_GoodClassStockPropertyEntity model = new T106_GoodClassStockPropertyEntity();
            if (reader["PID"] != DBNull.Value)
                model.PID = (int)reader["PID"];
            if (reader["PNo"] != DBNull.Value)
                model.PNo = (string)reader["PNo"];
            if (reader["PName"] != DBNull.Value)
                model.PName = (string)reader["PName"];
            if (reader["Color"] != DBNull.Value)
                model.Color = (string)reader["Color"];
            if (reader["icon"] != DBNull.Value)
                model.icon = (string)reader["icon"];
            if (reader["ParentNO"] != DBNull.Value)
                model.ParentNO = (string)reader["ParentNO"];
            if (reader["Descr"] != DBNull.Value)
                model.Descr = (string)reader["Descr"];
            if (reader["Status"] != DBNull.Value)
                model.Status = (EnumStatus)(byte)reader["Status"];
            if (reader["ProType"] != DBNull.Value)
                model.ProType = (T106_GoodClassStockPropertyEntity.EnumProType)(byte)reader["ProType"];
            if (reader["IsRequired"] != DBNull.Value)
                model.IsRequired = (bool)reader["IsRequired"];
            return model;
        }
        #endregion

        #region 根据PID获取一个对象
        public T106_GoodClassStockPropertyEntity GetObjectByPID(int PID)
        {
            string sql = "SELECT TOP 1 * FROM T106_GoodClassStockProperty  WHERE PID=@PID";
            var db = base.CreateDbReader();
            ParameterCollection parameters = CreateParameter();
            parameters.Append("PID", PID, DbType.Int32);
            try
            {
                IDataReader reader = db.ExecuteReader(sql, parameters);
                if (reader.Read())
                {
                    return this.FillDetailWithReader(reader);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return null;
        }
        #endregion

        #region 删除数据
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public int Delete(int PID)
        {
            var db = base.CreateDbWriter();
            string strSql = "DELETE T106_GoodClassStockProperty WHERE PID=@PID";
            ParameterCollection parameters = CreateParameter();
            parameters.Append("PID", PID);
            return db.ExecuteNonQuery(strSql.ToString(), parameters);
        }
        #endregion

        #region 数据集
        public IList<T106_GoodClassStockPropertyEntity> ShowList()
        {
            var db = base.CreateDbReader();
            string strSql = "SELECT  * FROM T106_GoodClassStockProperty";
            IList<T106_GoodClassStockPropertyEntity> result = null;
            result = db.Execute<T106_GoodClassStockPropertyEntity>(strSql);
            return result;
        }
        /// <summary>
        /// 获取库存属性
        /// </summary>
        /// <param name="cateNo"></param>
        /// <returns></returns>
        public IList<T106_GoodClassStockPropertyEntity> GetStockProperty(string cateNo)
        {
            var db = base.CreateDbReader();
            string strSql = @"select * from T106_GoodClassStockProperty where ParentNO in (SELECT pNO FROM    
                                T106_GoodClassStockProperty_Item
                            where GoodClassID=@cateNo)";
            ParameterCollection parameters = CreateParameter();
            parameters.Append("cateNo", cateNo);
            IList<T106_GoodClassStockPropertyEntity> result = null;
            result = db.Execute<T106_GoodClassStockPropertyEntity>(strSql, parameters);
            return result;
        }
        #endregion

        #region 数据集
        public DataTable ShowTable()
        {
            var db = base.CreateDbReader();
            string strSql = "SELECT  * FROM T106_GoodClassStockProperty";
            return db.ExecuteDataSet(strSql.ToString()).Tables[0];
        }
        #endregion

        #region 分页数据集
        public DataTable ShowTable(PageEntity entity, out int totalPages, out int totalCount)
        {
            BaseManager manager = new BaseManager();
            entity.tbName = "T106_GoodClassStockProperty";
            entity.PKey = "PID";
            entity.Orderby = "PID desc";
            DataTable dt = manager.GetTable(entity);
            totalPages = entity.TotalPages;
            totalCount = entity.TotalCount;
            return dt;
        }
        #endregion
    }
}
